const { host } = require('@/utils/host')
// const getSign = require('@/utils/getSign')
const { getConfig } = require('../../config')


const request = (type = "get", url, data = {}, config = { headers: {}, baseurl: '', code: false }) => {
  console.log(url, ' url')

  console.log(host(), ' host()')
  if (!url) {
    return Promise.reject(new Error(`url Can not be empty`))
  }
  return new Promise((resolve, reject) => {
    let defaultErr = (res) => {console.log(res)}
    let rError = getConfig().requestError['error'] || defaultErr

    uni.request({
      url: `${host()}${url}`,
      data: data,
      header: {  ...config.headers },
      method: type,
      success: (res) => {
        let req = res.data;
        let unlogin = getConfig().requestError['40001'] || defaultErr
        if (req.code === 0) {
          resolve(config.code ? req : req.data)
        } else if (req.code == 40001) {
          unlogin(req)
          reject(req)
          
          // setTimeout(() => {
          //   uni.navigateTo({
          //     url:'/pages/login/index?tag_id=86'
          //   })
          // }, 2000);
          // uni.showModal({
          //   title: "提示",
          //   content: "未登录或登录失效请登录！",
          //   cancelText: "取消",
          //   confirmText: "去登录",
          //   confirmColor: "#BFA559",
          //   success:(res) => {
          //     if (res.confirm) {
          //       uni.navigateTo({
          //         url:'/pages/login/index'
          //       })
          //     }
          //   }
          // });
        } else {
          rError(req)
          reject(req)
        }
      },
      fail: (err) => {
        rError(err)
        reject(err)
      },
    })
  })
}

module.exports = { request };